package com.sensePlusVR.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.main.R;

/**
 * @author: Loren
 * @create: 2016-7-1上午10:33:42
 * @desc: Activity 跳转工具
 * 
 */
public class MyPageJump {
	private static final String TAG = MyPageJump.class.getSimpleName();
	
	public static void pageJump(Context context, Class<?> toClass) {
		((Activity)context).startActivity(new Intent(context, toClass));
		((Activity)context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
	}
	
	public static void pageJumpAddString(Context context, Class<?> toClass,
			String key, String value){
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();
		bundle.putString(key, value);
		intent.putExtras(bundle);
		((Activity)context).startActivity(intent);
		((Activity)context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
	}
	
	public static String pageJumpGetString(Context context, String key) {
		if(context != null){
			Intent intent = ((Activity)context).getIntent();
			if(intent != null){
				Bundle bundle = intent.getExtras();
				if(bundle != null){
					return bundle.getString(key, "");
				}
			}
		}
		return "";
	}
	
	public static void pageJumpAddBoolean(Context context, Class<?> toClass,
			String key, boolean value){
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();
		bundle.putBoolean(key, value);
		intent.putExtras(bundle);
		((Activity)context).startActivity(intent);
		((Activity)context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
	}
	
	public static boolean pageJumpGetBoolean(Context context, String key) {
		if(context != null){
			Intent intent = ((Activity)context).getIntent();
			if(intent != null){
				Bundle bundle = intent.getExtras();
				if(bundle != null){
					return bundle.getBoolean(key, false);
				}
			}
		}
		return false;
	}

	public static void pageJumpAddInt(Context context, Class<?> toClass,
			String key, int value) {
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();
		bundle.putInt(key, value);
		intent.putExtras(bundle);
		((Activity)context).startActivity(intent);
		((Activity)context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
	}

	public static int pageJumpGetInt(Context context, String key) {
		return ((Activity)context).getIntent().getExtras().getInt(key, -1);
	}
	
/*	public static void pageJumpAddStringMap(Context context,Class<?> toClass,Map<String,String> map){
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();
		for(Map.Entry<String, String> entry : map.entrySet()){
			bundle.putString(entry.getKey(), entry.getValue());
		}
		intent.putExtras(bundle);
		((Activity)context).startActivity(intent);
		((Activity)context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
		
	}
	
	public static Map<String,String> pageJumpGetStringMap(Context context,List<String> keys){
		Map<String, String> map = new HashMap<String,String>();
		for(String key : keys){
			String value = (String) ((Activity)context).getIntent().getExtras().get(key);
			map.put(key, value);
		}
		return map;
		
	}*/

//	public static void pageJumpAddObject(Context context, Class<?> toClass,
//			String key, Object object) {
//		//如果传递的值为null也要跳转，在跳转后的界面判断数据信息
//		if(object == null){
//			Log.e(TAG, "pageJumpAddObject object == null");
//			pageJump(context, toClass);
//			return;
//		}
//		if (object instanceof UserDto) {
//			UserDto userDto = (UserDto) object;
//			Intent intent = new Intent(context, toClass);
//			Bundle bundle = new Bundle();
//			bundle.putSerializable(key, userDto);
//			intent.putExtras(bundle);
//			context.startActivity(intent);
//			((Activity) context).overridePendingTransition(
//					R.anim.in_anim, R.anim.out_anim);
//		} else if (object instanceof GameItem) {
//			GameItem gameItem = (GameItem) object;
//			Intent intent = new Intent(context, toClass);
//			Bundle bundle = new Bundle();
//			bundle.putParcelable(key, gameItem);
//			intent.putExtras(bundle);
//			context.startActivity(intent);
//			((Activity) context).overridePendingTransition(
//					R.anim.in_anim, R.anim.out_anim);
//		}else if (object instanceof LocalVideoItem) {
//			LocalVideoItem localVideoItem = (LocalVideoItem) object;
//			Intent intent = new Intent(context, toClass);
//			Bundle bundle = new Bundle();
//			bundle.putSerializable(key, localVideoItem);
//			intent.putExtras(bundle);
//			context.startActivity(intent);
//			((Activity) context).overridePendingTransition(
//					R.anim.in_anim, R.anim.out_anim);
//		}else if (object instanceof FollowItem) {
//			FollowItem followItem = (FollowItem) object;
//			Intent intent = new Intent(context, toClass);
//			Bundle bundle = new Bundle();
//			bundle.putSerializable(key, followItem);
//			intent.putExtras(bundle);
//			context.startActivity(intent);
//			((Activity) context).overridePendingTransition(
//					R.anim.in_anim, R.anim.out_anim);
//		}else if (object instanceof ActorItem) {
//			ActorItem actorItem = (ActorItem) object;
//			Intent intent = new Intent(context, toClass);
//			Bundle bundle = new Bundle();
//			bundle.putParcelable(key, actorItem);
//			intent.putExtras(bundle);
//			context.startActivity(intent);
//			((Activity) context).overridePendingTransition(
//					R.anim.in_anim, R.anim.out_anim);
//		}
//	}
	
	public static void pageJumpAddObject (Context context, Class<?> toClass,ActorItem actorItem,int fromPage){
			Intent intent = new Intent(context, toClass);
			Bundle bundle = new Bundle();
			bundle.putParcelable(Constant.INTENT_FOLLOW_DETAIL_ACTORITEM, actorItem);
			bundle.putInt(Constant.INTENT_FOLLOW_DETAIL_ACTOR_FROM, fromPage);
			intent.putExtras(bundle);
			context.startActivity(intent);
			((Activity) context).overridePendingTransition(
					R.anim.in_anim, R.anim.out_anim);
	}
	
	public static void pageJumpAddGameList(Context context,Class<?> toClass,ArrayList<GameItem> gameList){
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();
		bundle.putParcelableArrayList(Constant.INTENT_GAME_PLAY_LIST, gameList);
		intent.putExtras(bundle);
		((Activity)context).startActivity(intent);
		}
	
//	public static void pageJumpAddGameItem(Context context, Class<?> toClass,GameItem gameItem){
//		Intent intent = new Intent(context, toClass);
//		Bundle bundle = new Bundle();
//		bundle.putSerializable(Constant.INTENT_GAMEITEM, gameItem);
//		intent.putExtras(bundle);
//		((Activity)context).startActivity(intent);
//	}
	
	public static void pageJumpAddMap(Context context, Class<?> toClass,
			HashMap<String, String> maps) {
		if (maps == null)
			return;
		Intent intent = new Intent(context, toClass);
		Bundle bundle = new Bundle();

		Iterator<String> it = maps.keySet().iterator();
		while (it.hasNext()) {
			String key;
			String value;
			key = (String) it.next();
			value = maps.get(key);
			bundle.putString(key, value);
		}

		intent.putExtras(bundle);
		context.startActivity(intent);
		((Activity) context).overridePendingTransition(R.anim.in_anim,
				R.anim.out_anim);
	}
	
//	public static void pageJumpVideoDetailActivity(Context context,Class<?> toClass,GameItem gameItem,
//			CommendItem commendItem,boolean isShowFollowDetail){
//		
//		Intent intent = new Intent(context, VideoDetailActivity.class);
//		Bundle bundle = new Bundle();
//		bundle.putParcelable(Constant.INTENT_GAMEITEM, gameItem);
//		bundle.putParcelable(Constant.INTENT_COMMENDITEM, commendItem);
//		bundle.putBoolean(Constant.INTENT_SHOW_FOLLOW_DETAIL, isShowFollowDetail);
//		intent.putExtras(bundle);
//		context.startActivity(intent);
//		
//	}

	/**
	 * 根据包名打开指定已安装APP
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean startSpecifyActivity(Context context,
			String packageName) {
		Intent intent = context.getPackageManager().getLaunchIntentForPackage(
				packageName);
		if (intent != null) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			try {
				context.startActivity(intent);
				return true;
			} catch (ActivityNotFoundException e) {
				MyLog.e(TAG, "package not found.");
				return false;
			}
		}
		return false;
	}
	
	public static void pageJumpHome(Activity activity){
		 Intent intent = new Intent(Intent.ACTION_MAIN);
		 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 注意
	     intent.addCategory(Intent.CATEGORY_HOME);
	     activity.startActivity(intent);
	}
	
	public static void pageJumpWebSite(Activity activity,String url){
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		Uri content_url = Uri.parse(url);
		intent.setData(content_url);
		activity.startActivity(intent);
	}
}
